package com.woniuxy.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.woniuxy.admin.dto.PawnShopDto;
import com.woniuxy.admin.dto.PawnWarehouseDto;
import com.woniuxy.admin.entity.PawnWarehouse;
import com.woniuxy.admin.exception.PawnShopException;
import com.woniuxy.admin.form.InsertPawnWarehouseForm;
import com.woniuxy.admin.form.UpdatePawnWarehouseForm;
import com.woniuxy.admin.service.PawnWarehouseService;

import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/pawnWarehouse")
@Api(tags = "仓库接口")
public class PawnWarehouseController {

    @Resource
    private PawnWarehouseService pawnWarehouseService;

    /**
     * 分页查询仓库
     * @param pageNum
     * @param pageSize
     * @param condition
     * @return
     */
    @ApiOperation("分页查询仓库")
    @GetMapping("/loadAllPawnWarehouse")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value ="当前页码" ),
            @ApiImplicitParam(name = "pageSize",value ="每页几条" ),
            @ApiImplicitParam(name = "condition",value ="条件查询" )
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = PawnWarehouseDto.class)
    })
    public Result loadAllPawnWarehouse(Integer pageNum,Integer pageSize,String condition){
    Page<PawnWarehouseDto> pages= pawnWarehouseService.loadAllPawnWarehourse(pageNum,pageSize,condition);
    return Result.success("查询所有仓库成功",pages);
    }

    /**
     * 分页查询未绑定仓库
     * @param pageNum
     * @param pageSize
     * @param shopId
     * @return
     */
    @ApiOperation("分页查询未绑定仓库")
    @GetMapping("/findNoBingdingWhByShopId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value ="当前页码" ),
            @ApiImplicitParam(name = "pageSize",value ="每页几条" ),
            @ApiImplicitParam(name = "shopId",value ="店铺Id" )
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = PawnWarehouse.class)
    })

    public Result findNoBingdingWhByShopId(Integer pageNum,Integer pageSize,Integer shopId){
        Page<PawnWarehouse> pages= pawnWarehouseService.findNoBingdingWhByShopId(pageNum,pageSize,shopId);
        return Result.success("查询未绑定仓库成功",pages);
    }

    /**
     * 根据Id查询仓库
     * @param warehouseId
     * @return
     */
    @ApiOperation("根据Id查询仓库")
    @GetMapping("/findWhById")
    @ApiImplicitParam(name = "warehouseId",value ="仓库Id" )
    public Result findWhById(Integer warehouseId){
        PawnWarehouse pawnWarehouse = pawnWarehouseService.getById(warehouseId);
        if(pawnWarehouse==null) throw new PawnShopException(10001,"仓库不存在");
        return Result.success("根据Id查询仓库成功",pawnWarehouse);
    }

    /**
     * 新增仓库
     * @param form 仓库基本信息
     * @return
     */
    @ApiOperation("新增仓库")
    @PostMapping("/insertPawnWarehouse")
    @Permission(roles = {"超级管理员","管理员"})
    public Result insertPawnWarehouse(@RequestBody @Validated InsertPawnWarehouseForm form, BindingResult brs){
        if(brs.hasErrors()){
            throw new ParamException(10001,"参数异常");
        }
        pawnWarehouseService.insertPawnWarehouse(form);
        return Result.success("新增仓库成功",null);
    }

    /**
     * 修改仓库信息
     * @param form 仓库基本信息
     * @return
     */
    @ApiOperation("修改仓库信息")
    @PostMapping("/updatePawnWarehouse")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updatePawnWarehouse(@RequestBody @Validated UpdatePawnWarehouseForm form,BindingResult brs){
        if(brs.hasErrors()){
            throw new ParamException(10001,"参数异常");
        }
        pawnWarehouseService.updatePawnWarehouse(form);
        return Result.success("修改仓库信息成功",null);
    }

    /**
     * 更新仓库状态
     * @param warehouseId 仓库Id
     * @param state 仓库状态
     * @return
     */
    @ApiOperation("更新仓库状态")
    @GetMapping("/updatePawnWarehouseState")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId",value ="仓库ID" ),
            @ApiImplicitParam(name = "state",value ="仓库状态" )
    })
    @Permission(roles = {"超级管理员","管理员"})
    public Result updatePawnWarehouseState(Integer warehouseId,Integer state){
        pawnWarehouseService.updatePawnWarehouseState(warehouseId,state);
        return Result.success("更新仓库状态成功",null);
    }



}

